package com.zb.internetSpider.gui.tree;


import java.awt.Color;
import java.awt.Font;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import com.zb.internetSpider.entity.Menu;
import com.zb.internetSpider.gui.SpiderContainer;
import com.zb.internetSpider.gui.tree.listener.SpiderTreeSelectionListener;

public abstract class AbstractSpiderTree extends JTree  implements SpiderContainer {
	
	
	/**
	 * @Fields serialVersionUID : TODO
	 */
	private static final long serialVersionUID = 5632329806712026528L;
	
	protected DefaultTreeCellRenderer	cellRenderer = new DefaultTreeCellRenderer() ;
	
	protected List<Menu> menus;

	public abstract void addTreeModel(SpiderTreeModel spiderTreeModel) ;
	
	public abstract void addMenuData(List<? extends Object> menus);
	
	public AbstractSpiderTree  setClosedIcon(Icon icon) {
		this.cellRenderer.setClosedIcon(icon);
		return this;
	}
	
	public AbstractSpiderTree  setOpenIcon(Icon icon) {
		this.cellRenderer.setOpenIcon(icon);
		return this;
	}
	
	public AbstractSpiderTree  setLeafIcon(Icon icon) {
		this.cellRenderer.setLeafIcon(icon);
		return this;
	}
	

	@Override
	public void setFont(Font font, Color color) {
		// TODO Auto-generated method stub
		this.setFont(font);
		this.setForeground(color);
		cellRenderer.setFont(font);
		cellRenderer.setTextNonSelectionColor(color);
		cellRenderer.setTextSelectionColor(color);
		cellRenderer.setForeground(color);	
		cellRenderer.setLabelFor(null);
	}


	@Override
	public void setBackGroundColor(Color color) {
		// TODO Auto-generated method stub		
		this.setBackground(color);
		this.setBackgroundNonSelectionColor(color);
		this.setBackgroundSelectionColor(color);
		
	}
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		SpiderTreeNode root = new DefaultSpiderTreeNode("菜单树", true);	
		
		root = this.createTreeNode(root, menus);			
		SpiderTreeModel treeModel = new DefaultSpiderTreeModel(root);				
		super.setModel(treeModel);
		super.setCellRenderer(cellRenderer);
	}
	
	/**
	 * 
	 * @Description: TODO  把菜单数据生成数节点
	 * @param @param parentNode
	 * @param @param menus
	 * @param @return   
	 * @return SpiderTreeNode  
	 * @throws
	 * @author zb
	 * @date 2019年2月17日 下午5:46:04
	 */
	@SuppressWarnings("unchecked")
	private SpiderTreeNode createTreeNode(SpiderTreeNode parentNode, List<Menu> menus) {
		
		for(Menu menu : menus) {
			SpiderTreeNode spiderTreeNode = new DefaultSpiderTreeNode(menu.getMenuName(), true);
			if(menu.getChildren() != null && menu.getChildren().size() > 0)
				spiderTreeNode = this.createTreeNode(spiderTreeNode,(List<Menu>) menu.getChildren());
			/**
			 * 是叶子节点的时候设置节点状态
			 */
			if(menu.getChildren() == null || menu.getChildren().size() == 0) {
				if(menu.getMenuName().equals("菜单管理")){
					spiderTreeNode.setMenuManager(true);
				}else if(menu.getMenuName().equals("地址管理")){
					spiderTreeNode.setUrlManager(true);
				}
			}
			parentNode.add(spiderTreeNode);			
		}
		return parentNode;
	}
	
	public AbstractSpiderTree setBackgroundNonSelectionColor(Color color){
		cellRenderer.setBackgroundNonSelectionColor(color);
		return this;
	}
	
	public AbstractSpiderTree setBackgroundSelectionColor(Color color) {
		cellRenderer.setBackgroundSelectionColor(color);
		return this;
	}
	
	
	public abstract void addTreeSelectionEvent(SpiderTreeSelectionListener selectionListener);
	
}
